function a=backtk(cond,a0,x,p,c,f0,f1,rho)
% Backtrcing to determine the steplength of the line search method;
% Modified from Algorithm 3.1 in Numerical optimization (NW 2), p37;
% Adapted for all kinds of possible conditions.
% INPUT:
%       cond - the condition used to accept a step length (string);
%       a0 - the initial steplength (1*1);
%       x - current iterate (n*1);
%       p - search direction (n*1);
%       c - constant provided by user, for armijo condition (1*1);
%       f0 - function handle of the objective function (Rn->R);
%       f1 - the gradient of the function (Rn->Rn);
%       rho - the multiplier for the backtracking, less than 1 (1*1);
% OUTPUT:
%       a - the accepted steplength (1*1);
% REVISION:
%       jqin, 15/feb/2011, created;
switch cond
    case 'armijo'
        check=@(a)armijo(a,x,p,c,f0,f1);
    case 'goldstein'
        check=@(a)goldstein(a,x,p,c,f0,f1);
    case 'wolfe'
        check=@(a)wolfe(a,x,p,c,f1);
    case 'swolfe'
        check=@(a)swolfe(a,x,p,c,f1);
    case 'gamma'
        check=@(a)gamma(a,x,p,c(1),c(2),f0,f1);
end
a=a0;
while check(a)==0  % specified condition doesn't hold
    a=rho*a;
end
    